﻿using _8Group.MES.IAppService.ToolType;
using _8Group.MES.IAppService.ToolType.Dtos;
using _8Group.MES.IAppService.Warehouses;
using _8Group.MES.IAppService.Warehouses.Dtos;
using _8Group.MES.Model.TM工装夹具管理;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.AppService
{
    /// <summary>
    /// 工具类型实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "周扬")]
    public class ToolTypeAppService : MESAppService, IToolTypeAppService
    {
        private readonly IRepository<TM_Tool_Type, int> _toolTypeRepository;

        public ToolTypeAppService(IRepository<TM_Tool_Type, int> toolTypeRepository)
        {
            _toolTypeRepository = toolTypeRepository;
        }

        /// <summary>
        /// 添加工具类型
        /// </summary>
        /// <param name="input">工具类型信息</param>
        /// <returns></returns>
        public async Task<int> AddToolType(CreateToolTypeDto input)
        {
            var toolType = AutoMapperExt.MapTo<TM_Tool_Type>(input);
            toolType.CreateTime = DateTime.Now;
            await _toolTypeRepository.InsertAsync(toolType);
            return 1;
        }

        /// <summary>
        /// 修改工具类型
        /// </summary>
        /// <param name="input">工具类型信息</param>
        /// <returns></returns>
        public async Task<bool> UpdateToolType(UpdateToolTypeDto input)
        {
            var toolType = AutoMapperExt.MapTo<TM_Tool_Type>(input);
            await _toolTypeRepository.UpdateAsync(toolType);
            return true;
        }

        /// <summary>
        /// 删除工具类型
        /// </summary>
        /// <param name="id">工具类型编号</param>
        /// <returns></returns>
        public async Task<bool> DeleteToolType(int id)
        {
            var toolType = await _toolTypeRepository.GetAsync(id);
            await _toolTypeRepository.DeleteAsync(toolType, true);
            return true;
        }

        /// <summary>
        /// 显示工具类型信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetToolTypes( string? toolTypeName, string? maintenType, int pageIndex = 1, int pageSize = 10)
        {
            var toolTypes = await _toolTypeRepository.GetQueryableAsync();

            toolTypes = toolTypes.WhereIf(!string.IsNullOrWhiteSpace(toolTypeName), x => x.ToolTypeName.Contains(toolTypeName));
            toolTypes = toolTypes.WhereIf(!string.IsNullOrWhiteSpace(maintenType), x => x.MaintenType==maintenType);

            int totalCount = toolTypes.Count();
            toolTypes = toolTypes.OrderByDescending(x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            var result = new ResponseModel
            {
                data = toolTypes.ToList(),
                code = "0",
                count = totalCount,
                msg = "请求成功"
            };

            return JsonConvert.SerializeObject(result, Formatting.Indented);
        }
    }
}
